/* comment: define html control [select(dropdownlist)] control class
 *           public attributes:  ctrlObj [get select control object]
 *                               selectedValue [get selected item value] 
 *                               selectedText [get selected item text]
 *           public interface:   setSelectedValue [set selected item by value]
 *                               setSelectedIndex [set selected item by index]
 *                               addItem [add new item to bottom]
 *                               clear [clear all items]
 * coder: tim 
  ***/                          
var DropDownList = function (ctrlId){
	this.ctrlId = ctrlId;
	if(ctrlId != "") {
		this.__getCtrlObject();
	}
	
	this.selectedValue = this.__getSelectedValue();
	this.selectedText = this.__getSelectedText();
}

/* comment: html select control [dropdonwlist] public interface 
 * coder:   tim
 * */

// set select control selected value
DropDownList.prototype.setSelectedValue = function(selectedValue) {
	if(null == selectedValue || selectedValue == "undefined") return;
	
	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	var oOption
	
	for(var i = 0; i < icount; i ++) {
		oOption = optionArray[i];
		if(selectedValue == oOption.value) {
			oOption.selected = true;			
			break;
		}
	}	
}

// set select control selected item by index
DropDownList.prototype.setSelectedIndex = function(selectedIndex) {
	if(null == selectedIndex || selectedIndex == "undefined") return;
	
	if(selectedIndex > (this.ctrlObj.options.length - 1)) return;
	
	this.ctrlObj.selectedIndex = selectedIndex;
}

// append single item in select control options bottom
DropDownList.prototype.addItem = function(itemText,itemValue) {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	//var newOption = new Option(itemText,itemValue);
	var newOption = new Option;
	this.ctrlObj.add(newOption.selfObj);	
	newOption.selfObj.innerText = itemText;
	newOption.selfObj.value = itemValue;
}

// clear select control's all option item
DropDownList.prototype.clear = function() {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	for(var i = (icount - 1); i >= 0; i --) {
		optionArray.remove(i);
	}
}
/* public interface defined end */


/* comment: html select control [dropdownlist] private interface
 * coder:   tim
 * */

// get select control object interface
DropDownList.prototype.__getCtrlObject = function() {
	this.ctrlObj = document.getElementById(this.ctrlId);	
}

// get select control selected value interface
DropDownList.prototype.__getSelectedValue = function() {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var selectedOption = this.__getSelectOption();
	if(null == selectedOption || selectedOption == "undefined") return "";
	
	return selectedOption.value;
}

// get select control selected text interface
DropDownList.prototype.__getSelectedText = function() {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var selectedOption = this.__getSelectOption();
	if(null == selectedOption || selectedOption == "undefined") return "";
	
	return selectedOption.text;
}
 
// get select control selected option object
DropDownList.prototype.__getSelectOption = function() {
	var selectedIndex = this.ctrlObj.selectedIndex;
  if(selectedIndex == "undefined" || selectedIndex == -1) return "";
                
  var selectedOption = this.ctrlObj.options[selectedIndex];
  
  return selectedOption;
}

/* comment: define create option control class
 *          commonly used in add option control to select control
 * coder: tim
 */
var Option = function(itemText,itemValue) {		
	if(null == itemText || null == itemValue) {
		this.selfObj = document.createElement("<option>");
	}
  else {  	 
		this.selfObj = document.createElement("<option value='" + itemValue + "'>");
		this.selfObj.innerText = itemText;	
	}
}



/* comment: define html control [radiobuttonlist] control class
 *           public attributes:  ctrlObjArray [get radio array object]
 *                               checkedValue [get checked item value] 
 *           public interface:   setCheckedItem [set checked item by value] 
 * coder: tim 
  ***/          
var RadioList = function(ctrlId) {
	this.ctrlId = ctrlId;
	if(ctrlId != "")
		this.__getCtrlObject();
	
	this.checkedValue = this.__getCheckedValue();
}

/* comment: radio list control class public interface
 * coder: tim
 */

/// set radio list checked item
RadioList.prototype.setCheckedItem = function(checkedValue) {
	if(null == this.ctrlObjArray || this.ctrlObjArray == "undefined") this.__getCtrlObject();
	
	var icount = this.ctrlObjArray.length;
	var oRadio;
	for(var i =0; i < icount; i ++){
		oRadio = this.ctrlObjArray[i];
		if((oRadio.type == "radio") && (oRadio.value == checkedValue)){
			oRadio.checked = true;
			break;
		}
	} 
}

/* comment: radio list control class private interface
 * coder: tim
 */

/// get radio list object array
RadioList.prototype.__getCtrlObject = function() {
	this.ctrlObjArray = document.getElementsByName(this.ctrlId);	
}

/// get radio list checked value
RadioList.prototype.__getCheckedValue = function() {
	if(null == this.ctrlObjArray || this.ctrlObjArray == "undefined") this.__getCtrlObject();
	
	var icount = this.ctrlObjArray.length;
	var oRadio;
	var checkedValue = "";
	for(var i =0; i < icount; i ++){
		oRadio = this.ctrlObjArray[i];
		if((oRadio.type == "radio") && oRadio.checked){
			checkedValue = oRadio.value;
			break;
		}
	} 
	return checkedValue;
}


/* comment: define html control [listbox] control class
 *           public attributes:  ctrlObj [get listbox control object]
 *                               selectedValue [get single selected item value] 
 *                               selectedText [get single selected item text]
 *                               multiSelValue [get multi selected item value] 
 *                               multiSelText [get multi selected item text]
 *           public interface:   addItem [add new item to bottom]
 *                               addItems [add multi new item to bottom]
 *                               moveSelItem [move selected items return muti selected items
 *                                            and remove selected items]
 *                               clearItems [remove defined items]
 *                               clearAll [remove all items]
 * coder: tim 
  ***/    
var ListBox = function(ctrlId){
	this.ctrlId = ctrlId;
	if(ctrlId != "")
		this.__getCtrlObject();
		
	if(!this.ctrlObj.multiple) {
		this.selectedValue = this.__getSelectedValue();
		this.selectedText = this.__getSelectedText();	
	}
	else {
		this.multiSelValue = this.__getMultiSelValue();
		this.multiSelText = this.__getMultiSelText();	
	}
}

/* comment: listbox[select] control class public interface
 * coder: tim
 */

/// append new option to listbox items bottom
ListBox.prototype.addItem = function(itemText,itemValue){
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var newOption = new Option;		
	this.ctrlObj.add(newOption.selfObj);
	newOption.selfObj.innerText = oitem.Text;
	newOption.selfObj.value = oitem.Value;
}

/// append multi new option item
ListBox.prototype.addItems = function(itemArray){
	if(null == itemArray || itemArray == "undefined" || itemArray.length == 0) return;	
	
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var icount = itemArray.length;
	var oitem;
	var newOption;
	for(var i = 0; i < icount; i ++){
		oitem = itemArray[i];
		newOption = new Option;		
		this.ctrlObj.add(newOption.selfObj);
		newOption.selfObj.innerText = oitem.Text;
		newOption.selfObj.value = oitem.Value;
	}
}


/// return selected items array and clear selected item
ListBox.prototype.moveSelItem = function(isAll,isRemoveSel) {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	var moveArray = this.__getSelOpions(isAll);
	if(isRemoveSel){
		if(isAll)
			this.clearAll();
		else {
			var moveIndexArray = this.__getSelIndexes(isAll);
			this.clearItems(moveIndexArray);
		
		}
	}
	
	return moveArray;
}

/// clear defined items
ListBox.prototype.clearItems = function(removeIndexArray) {
	if((null == removeIndexArray) || (removeIndexArray == "undefined") || (removeIndexArray.length == 0)) return;
	
	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	var oOption;
	for(var i = 0; i < removeIndexArray.length; i ++){
		this.ctrlObj.options.remove(removeIndexArray[i]);		
	}
}

/// clear all items
ListBox.prototype.clearAll = function(){
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	for(var i = (icount - 1); i >= 0 ; i --){
		this.ctrlObj.options.remove(i);
	}	
}

/* comment: listbox[select] control class private interface
 * coder: tim
 */


/// get listbox control object 
ListBox.prototype.__getCtrlObject = function() {
	this.ctrlObj = document.getElementById(this.ctrlId);	
}

/// get listbox control selected value interface in single select mode
ListBox.prototype.__getSelectedValue = function() {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var selectedOption = this.__getSelectOption();
	if(null == selectedOption || selectedOption == "undefined") return "";
	
	return selectedOption.value;
}

/// get listbox control selected text interface in single select mode
ListBox.prototype.__getSelectedText = function() {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var selectedOption = this.__getSelectOption();
	if(null == selectedOption || selectedOption == "undefined") return "";
	
	return selectedOption.text;
}
 
/// get listbox control selected option object in single select mode
ListBox.prototype.__getSelectOption = function() {
  var selectedIndex = this.ctrlObj.selectedIndex;
  if(selectedIndex == "undefined" || selectedIndex == -1) return "";
                
  var selectedOption = this.ctrlObj.options[selectedIndex];
  
  return selectedOption;
}

/// get listbox selected item array in multi select mode
ListBox.prototype.__getSelOpions = function(isAll){
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	var oOption;
	var dicItem;
	var retArray = [];
	for(var i = 0; i < icount; i ++){
		oOption = optionArray[i];
		if(isAll){
			dicItem = new ListItem(oOption.innerText,oOption.value);			
			retArray.push(dicItem);
		}
		else{
			if(oOption.selected) {
				dicItem = new ListItem(oOption.innerText,oOption.value);			
				retArray.push(dicItem);
			}	
		}					
	}	
	return retArray;
}

/// get listbox selected item index array in multi select mode
ListBox.prototype.__getSelIndexes = function(isAll) {
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();
	
	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	var oOption;	
	var indexArray = [];
	for(var i = icount - 1; i >= 0 ; i --){
		oOption = optionArray[i];
		if(isAll)			
			indexArray.push(i);		
		else{
			if(oOption.selected) indexArray.push(i);				
		}					
	}	
	return indexArray;	
}

/// get listbox multi selected value (separator: ",")
ListBox.prototype.__getMultiSelValue = function(){	
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();

	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	var oOption;
	var selArrayId = "";
	for(var i = 0; i < icount; i ++){
		oOption = optionArray[i];
		if(oOption.selected)
			selArrayId += (selArrayId == "") ? oOption.value : "," + oOption.value;
	}
	
	return selArrayId;
}

/// get listbox multi selected text (separator: " ")
ListBox.prototype.__getMultiSelText = function(){
	if(null == this.ctrlObj || this.ctrlObj == "undefined") this.ctrlObj = this.__getCtrlObject();

	var optionArray = this.ctrlObj.options;
	var icount = optionArray.length;
	var oOption;
	var selArrayText = "";
	for(var i = 0; i < icount; i ++){
		oOption = optionArray[i];
		if(oOption.selected)
			selArrayText += (selArrayText == "") ? oOption.innerText : " " + oOption.innerText;
	}
	
	return selArrayText;
}


var ListItem = function(itemText,itemValue){
	this.Text = itemText;
	this.Value = itemValue;
}
